<template>
  <el-card class="form-container" shadow="never">
    <el-form
      :model="device"
      :rules="rules"
      ref="bannerFrom"
      label-width="150px"
      size="small"
    >
      <el-form-item label="设备代码:">
        <el-input v-model="device.deviceCode" placeholder="网关设备房间随机填写唯一的字符串" class="input-width"></el-input>
      </el-form-item>
<!--      <el-form-item label="区域:">-->
<!--        <el-input v-model="device.area" class="input-width"></el-input>-->
<!--      </el-form-item>-->
      <el-form-item label="房间名:">
        <el-input v-model="device.machineName" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="描述:">
        <el-input type="textarea" v-model="device.deviceDesc" class="input-width" style="height: 50px"></el-input>
      </el-form-item>
      <el-form-item label="推荐套餐:">
        <el-input v-model="device.customerMeal" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="房间图片:">
        <single-upload v-model="device.imageUrl"></single-upload>
      </el-form-item>
      <el-form-item label="房间图片:">
        <single-upload v-model="device.imageUrl1"></single-upload>
      </el-form-item>
      <el-form-item label="房间图片:">
        <single-upload v-model="device.imageUrl2"></single-upload>
      </el-form-item>
      <el-form-item label="排序:">
        <el-input v-model="device.sort" class="input-width"></el-input>
      </el-form-item>
      <el-form-item label="设备类型:">
        <el-radio-group v-model="device.deviceType">
          <el-radio :label="0">4G设备</el-radio>
          <el-radio :label="1">4G+wifi设备</el-radio>
          <el-radio :label="2">wifi锁</el-radio>
          <el-radio :label="3">谷家wifi锁</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否是大门:">
        <el-radio-group v-model="device.isMealdevice">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否是网关设备:">
        <el-radio-group v-model="device.isGateway">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="续费单价:">
        <el-input v-model="device.price" class="input-width" placeholder="数字类型,最多保留小数点后两位"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('bannerFrom')"
          >提交</el-button
        >
        <el-button v-if="!isEdit" @click="resetForm('bannerFrom')"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>
<script>
import SingleUpload from "@/components/Upload/singleUpload";
import { createDevice, updateDevice, getDevice } from "@/api/qsg/device";

const defaultDevice = {
  deviceCode: '',
  machineName: '',
  area: ''
};

export default {
  name: "DeviceDetail",
  components: { SingleUpload },
  props: {
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      device: null,
    };
  },
  created() {
    if (this.isEdit) {
      getDevice(this.$route.query.id).then((response) => {
        this.device = response.data;
      });
    } else {
      this.device = Object.assign({}, defaultDevice);
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm("是否提交数据", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }).then(() => {
            if (this.isEdit) {
              updateDevice(this.$route.query.id, this.device).then(
                (response) => {
                  this.$refs[formName].resetFields();
                  this.$message({
                    message: "修改成功",
                    type: "success",
                    duration: 1000,
                  });
                  this.$router.back();
                }
              );
            } else {
              createDevice(this.device).then((response) => {
                this.$refs[formName].resetFields();
                this.device = Object.assign({}, defaultDevice);
                this.$message({
                  message: "提交成功",
                  type: "success",
                  duration: 1000,
                });
              });
            }
          });
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000,
          });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.device = Object.assign({}, defaultDevice);
    },
  },
};
</script>
<style scoped>
.input-width {
  width: 70%;
}
</style>


